package cn.hahah.module.blog.service.usercomment;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.usercomment.vo.*;
import cn.hahah.module.blog.dal.dataobject.usercomment.UserCommentDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.usercomment.UserCommentMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户评论文章 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserCommentServiceImpl implements UserCommentService {

    @Resource
    private UserCommentMapper userCommentMapper;

    @Override
    public Long createUserComment(UserCommentSaveReqVO createReqVO) {
        // 插入
        UserCommentDO userComment = BeanUtils.toBean(createReqVO, UserCommentDO.class);
        userCommentMapper.insert(userComment);
        // 返回
        return userComment.getId();
    }

    @Override
    public void updateUserComment(UserCommentSaveReqVO updateReqVO) {
        // 校验存在
        validateUserCommentExists(updateReqVO.getId());
        // 更新
        UserCommentDO updateObj = BeanUtils.toBean(updateReqVO, UserCommentDO.class);
        userCommentMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserComment(Long id) {
        // 校验存在
        validateUserCommentExists(id);
        // 删除
        userCommentMapper.deleteById(id);
    }

    private void validateUserCommentExists(Long id) {
        if (userCommentMapper.selectById(id) == null) {
            throw exception(USER_COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public UserCommentDO getUserComment(Long id) {
        return userCommentMapper.selectById(id);
    }

    @Override
    public PageResult<UserCommentDO> getUserCommentPage(UserCommentPageReqVO pageReqVO) {
        return userCommentMapper.selectPage(pageReqVO);
    }

}